package com.mdy.mq.sender.service;

import com.google.gson.Gson;
import com.mdy.mq.bean.MqMessage;
import com.mdy.mq.bean.Person;
import com.mdy.mq.sender.modle.MqConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author mengdy
 */
@Service
@Slf4j
public class MqSenderService {
    Gson mGson = new Gson();
    @Autowired
    RabbitTemplate rabbitTemplate;
    public MqSenderService() {
    }
    public String pushXxxEvent(Person req,Integer time, Integer num) {
        log.info("pushing req into mq queue: {}", req);
        String json = mGson.toJson(req);
        log.info("pushing json into mq queue: {}", json);
        int i = 0;
        while (i++ < num){
            try {
                rabbitTemplate.convertAndSend(MqConstant.DIRECT_EXCHANGE_NAME_2,MqConstant.DIRECT_KEY_NAME_2, req);
                Thread.sleep(time);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        return "success";
    }
}
